
' TCheckBox
Type TCheckBox Extends TWidget
	Field checked:Int
	Field textSizeX:Int
	Field textSizeY:Int
	Field boxWidth:Int
	Field boxHeight:Int
	
	' Init
	Method Init(nID:String, nText:String, nX:Int, nY:Int, nWidth:Int, nHeight:Int)
		Self.InitWidget(nID, nText, nX, nY, nWidth, nHeight)
		Self.checked = 0
		Self.boxWidth = 16
		Self.boxHeight = 16
	End Method
	
	' SetState
	Method SetState(nChecked:Int)
		Self.checked = nChecked
		
		' TODO: Change handler
		If Self.onClick
			Self.onClick(Self)
		EndIf
	End Method
	
	' GetState
	Method GetState:Int()
		Return Self.checked
	End Method
	
	' ToggleState
	Method ToggleState() 
		Self.SetState(1 - Self.checked)
	End Method
	
	' SetText
	Method SetText(nText:String)
		Self.text = nText
		Self.textSizeX = TextWidth(nText)
		Self.textSizeY = TextHeight(nText)
		Self.SetSizeAbs(Self.boxWidth + Self.textSizeX + 5, Max(Self.boxHeight, Self.textSizeY))
	End Method
	
	' Update
	Method Update()
		Self.hovered = False
		
		If PointInRect(TInputSystem.GetMouseX(), TInputSystem.GetMouseY(), Self.screenX, Self.screenY, Self.rWidth, Self.rHeight)
			Self.hovered = True
			
			If TInputSystem.GetMouseHit(1)
				Self.ToggleState()
				
				' OnClick handler
				If Self.onClick <> Null
					Self.onClick(Self.GetAffectingWidget())
				EndIf
			EndIf
		EndIf
	End Method
	
	' Draw
	Method Draw()
		.SetColor Self.r, Self.g, Self.b
		DrawOval Self.rX, Self.rY, Self.textSizeY, Self.textSizeY
		
		If Self.checked
			.SetColor 0, 0, 0
			DrawOval Self.rX + 1, Self.rY + 1, Self.textSizeY - 2, Self.textSizeY - 2
		EndIf
		
		If Self.hovered
			.SetAlpha 0.5
			DrawOval Self.rX, Self.rY, Self.textSizeY, Self.textSizeY
		EndIf
		
		.SetColor Self.textR, Self.textG, Self.textB
		DrawText Self.text, Self.rX + Self.boxWidth + 5, Self.rY + Self.rHeight / 2 - Self.textSizeY / 2
	End Method
	
	' Create
	Function Create:TCheckBox(nID:String, nText:String, nX:Int, nY:Int, nWidth:Int = 0, nHeight:Int = 0)
		Local widget:TCheckBox = New TCheckBox
		widget.Init(nID, nText, nX, nY, nWidth, nHeight)
		Return widget
	End Function
End Type
